#delimit;

****;
version 9.0;

clear;
clear matrix;
estimates clear;
eststo clear;
set more off;
set mem 400m;
set seed 365476247;

*********Children;

cap prog drop runme;
prog def runme;

local hypothesis = `0';
tempfile main bootsave;

use "$da/Crimes-Against-Morality_Child_data-constructed.dta", clear;

*2. Setting Macros;
local sw1 = "if el==1&sw==1&male!=.";
local sw2 = "if el==1";
local sw3 = "if el==1&sw==0";


di ;

reg $var closing `char' `sw1', cluster(wsid);
global mainbeta = _b[closing];
global maint = (_b[closing]-`hypothesis')/_se[closing];
predict epshat, resid;
predict yhat, xb;

*also general the "impose the null hypothesis" yhat and residual;
qui gen temp_y = $var - closing*`hypothesis';
reg temp_y `sw1';
predict epshat_imposed, resid;
predict yhat_imposed, xb;
qui replace yhat_imposed = yhat_imposed + closing*`hypothesis';

sort wsid el closing;
qui save `main', replace;

*getting a count of then number of clusters in the sample;
qui by wsid: keep if _n==1;
qui summ;
global numclust = r(N);

cap erase `bootsave';
qui postfile bskeep beta_np t_np t_wild
	using `bootsave', replace;

forvalues b=1/$bootreps { ;

qui use `main', replace;
qui by wsid: gen tempbs = uniform();
qui by wsid: gen pos = (tempbs[1] <.5);

qui gen wildresid = epshat_imposed *(2*pos - 1);
qui gen wildy = yhat_imposed + wildresid;
qui reg wildy closing `sw1', cluster(wsid);
local bst_wild =(_b[closing]-`hypothesis')/_se[closing];

*nonparametric bootstrap;
bsample, cluster(wsid) idcluster(newsid);
qui reg $var closing `sw1', cluster(newsid);
local bsbeta = _b[closing];
local bst = (_b[closing]-$mainbeta)/_se[closing];

post bskeep (`bsbeta') (`bst') (`bst_wild');
};
*;
qui postclose bskeep;

qui drop _all;
qui set obs 1;
qui gen t_np = $maint;
qui gen t_wild = $maint;
qui append using `bootsave';

qui gen n = .;

foreach stat in t_np t_wild {;
qui summ `stat';
local bign = r(N);
sort `stat';
qui replace n = _n;
qui summ n if abs(`stat'-$maint)< 0.000001;
local myp = r(mean) / `bign';
global pctile_`stat' = 2*min(`myp', (1-`myp'));
};
*;

global mainp = norm($maint);
global pctile_main = 2*min($mainp, (1-$mainp));

local myfmt = "%7.5f";

di;
di "Number BS Reps = $bootreps, Null hypothesis = `hypothesis'";
display "Main beta" _column(13) "main T" _column(22) "Main %le" 
		_column(33) "NP BS %le" _column(44) "wild %le";
di %6.3f $mainbeta _column(13) %6.3f $maint _column(23) 
		`myfmt' $pctile_main _column(34) `myfmt' $pctile_t_np 
		_column(44) `myfmt' $pctile_t_wild ;
		
end;

global bootreps = 999;
runme 0;


***************************************;

*closing;

cap prog drop runme;
prog def runme;

local hypothesis = `0';
tempfile main bootsave;

use "$da/Crimes-Against-Morality_Child_data-constructed.dta", clear;

*2. Setting Macros;
local sw1 = "if el==1&sw==1&male!=.";
local sw2 = "if el==1";
local sw3 = "if el==1&sw==0";


di ;

reg $var closing clos_male male `char' `sw1', cluster(wsid);
global mainbeta = _b[closing];
global maint = (_b[closing]-`hypothesis')/_se[closing];
predict epshat, resid;
predict yhat, xb;

*also general the "impose the null hypothesis" yhat and residual;
qui gen temp_y = $var - closing*`hypothesis';
reg temp_y clos_male male `sw1';
predict epshat_imposed, resid;
predict yhat_imposed, xb;
qui replace yhat_imposed = yhat_imposed + closing*`hypothesis';

sort wsid el closing;
qui save `main', replace;

*getting a count of then number of clusters in the sample;
qui by wsid: keep if _n==1;
qui summ;
global numclust = r(N);

cap erase `bootsave';
qui postfile bskeep beta_np t_np t_wild
	using `bootsave', replace;

forvalues b=1/$bootreps { ;

qui use `main', replace;
qui by wsid: gen tempbs = uniform();
qui by wsid: gen pos = (tempbs[1] <.5);

qui gen wildresid = epshat_imposed *(2*pos - 1);
qui gen wildy = yhat_imposed + wildresid;
qui reg wildy closing clos_male male `sw1', cluster(wsid);
local bst_wild =(_b[closing]-`hypothesis')/_se[closing];

*nonparametric bootstrap;
bsample, cluster(wsid) idcluster(newsid);
qui reg $var closing clos_male male `sw1', cluster(newsid);
local bsbeta = _b[closing];
local bst = (_b[closing]-$mainbeta)/_se[closing];

post bskeep (`bsbeta') (`bst') (`bst_wild');
};
*;
qui postclose bskeep;

qui drop _all;
qui set obs 1;
qui gen t_np = $maint;
qui gen t_wild = $maint;
qui append using `bootsave';

qui gen n = .;

foreach stat in t_np t_wild {;
qui summ `stat';
local bign = r(N);
sort `stat';
qui replace n = _n;
qui summ n if abs(`stat'-$maint)< 0.000001;
local myp = r(mean) / `bign';
global pctile_`stat' = 2*min(`myp', (1-`myp'));
};
*;

global mainp = norm($maint);
global pctile_main = 2*min($mainp, (1-$mainp));

local myfmt = "%7.5f";

di;
di "Number BS Reps = $bootreps, Null hypothesis = `hypothesis'";
display "Main beta" _column(13) "main T" _column(22) "Main %le" 
		_column(33) "NP BS %le" _column(44) "wild %le";
di %6.3f $mainbeta _column(13) %6.3f $maint _column(23) 
		`myfmt' $pctile_main _column(34) `myfmt' $pctile_t_np 
		_column(44) `myfmt' $pctile_t_wild ;
		
end;

global bootreps = 999;
runme 0;

*clos_male;

cap prog drop runme;
prog def runme;

local hypothesis = `0';
tempfile main bootsave;

use "$da/Crimes-Against-Morality_Child_data-constructed.dta", clear;

*2. Setting Macros;
local sw1 = "if el==1&sw==1&male!=.";
local sw2 = "if el==1";
local sw3 = "if el==1&sw==0";


di ;

reg $var closing clos_male male `char' `sw1', cluster(wsid);
global mainbeta = _b[clos_male];
global maint = (_b[clos_male]-`hypothesis')/_se[clos_male];
predict epshat, resid;
predict yhat, xb;

*also general the "impose the null hypothesis" yhat and residual;
qui gen temp_y = $var - clos_male*`hypothesis';
reg temp_y closing male `sw1';
predict epshat_imposed, resid;
predict yhat_imposed, xb;
qui replace yhat_imposed = yhat_imposed + clos_male*`hypothesis';

sort wsid closing male;
qui save `main', replace;

*getting a count of then number of clusters in the sample;
qui by wsid: keep if _n==1;
qui summ;
global numclust = r(N);

cap erase `bootsave';
qui postfile bskeep beta_np t_np t_wild
	using `bootsave', replace;

forvalues b=1/$bootreps { ;

qui use `main', replace;
qui by wsid: gen tempbs = uniform();
qui by wsid: gen pos = (tempbs[1] <.5);

qui gen wildresid = epshat_imposed *(2*pos - 1);
qui gen wildy = yhat_imposed + wildresid;
qui reg wildy closing clos_male male `sw1', cluster(wsid);
local bst_wild =(_b[clos_male]-`hypothesis')/_se[clos_male];

*nonparametric bootstrap;
bsample, cluster(wsid) idcluster(newsid);
qui reg $var closing clos_male male `sw1', cluster(newsid);
local bsbeta = _b[clos_male];
local bst = (_b[clos_male]-$mainbeta)/_se[clos_male];

post bskeep (`bsbeta') (`bst') (`bst_wild');
};
*;
qui postclose bskeep;

qui drop _all;
qui set obs 1;
qui gen t_np = $maint;
qui gen t_wild = $maint;
qui append using `bootsave';

qui gen n = .;

foreach stat in t_np t_wild {;
qui summ `stat';
local bign = r(N);
sort `stat';
qui replace n = _n;
qui summ n if abs(`stat'-$maint)< 0.000001;
local myp = r(mean) / `bign';
global pctile_`stat' = 2*min(`myp', (1-`myp'));
};
*;

global mainp = norm($maint);
global pctile_main = 2*min($mainp, (1-$mainp));

local myfmt = "%7.5f";

di;
di "Number BS Reps = $bootreps, Null hypothesis = `hypothesis'";
display "Main beta" _column(13) "main T" _column(22) "Main %le" 
		_column(33) "NP BS %le" _column(44) "wild %le";
di %6.3f $mainbeta _column(13) %6.3f $maint _column(23) 
		`myfmt' $pctile_main _column(34) `myfmt' $pctile_t_np 
		_column(44) `myfmt' $pctile_t_wild ;
		
end;

global bootreps = 999;
runme 0;

*male;

cap prog drop runme;
prog def runme;

local hypothesis = `0';
tempfile main bootsave;

use "$da/Crimes-Against-Morality_Child_data-constructed.dta", clear;

*2. Setting Macros;
local sw1 = "if el==1&sw==1&male!=.";
local sw2 = "if el==1";
local sw3 = "if el==1&sw==0";


di ;

reg $var closing clos_male male `char' `sw1', cluster(wsid);
global mainbeta = _b[male];
global maint = (_b[male]-`hypothesis')/_se[male];
predict epshat, resid;
predict yhat, xb;

*also general the "impose the null hypothesis" yhat and residual;
qui gen temp_y = $var - clos_male*`hypothesis';
reg temp_y clos_male closing `sw1';
predict epshat_imposed, resid;
predict yhat_imposed, xb;
qui replace yhat_imposed = yhat_imposed + male*`hypothesis';

sort wsid closing male;
qui save `main', replace;

*getting a count of then number of clusters in the sample;
qui by wsid: keep if _n==1;
qui summ;
global numclust = r(N);

cap erase `bootsave';
qui postfile bskeep beta_np t_np t_wild
	using `bootsave', replace;

forvalues b=1/$bootreps { ;

qui use `main', replace;
qui by wsid: gen tempbs = uniform();
qui by wsid: gen pos = (tempbs[1] <.5);

qui gen wildresid = epshat_imposed *(2*pos - 1);
qui gen wildy = yhat_imposed + wildresid;
qui reg wildy closing clos_male male `sw1', cluster(wsid);
local bst_wild =(_b[male]-`hypothesis')/_se[male];

*nonparametric bootstrap;
bsample, cluster(wsid) idcluster(newsid);
qui reg $var closing clos_male male `sw1', cluster(newsid);
local bsbeta = _b[male];
local bst = (_b[male]-$mainbeta)/_se[clos_male];

post bskeep (`bsbeta') (`bst') (`bst_wild');
};
*;
qui postclose bskeep;

qui drop _all;
qui set obs 1;
qui gen t_np = $maint;
qui gen t_wild = $maint;
qui append using `bootsave';

qui gen n = .;

foreach stat in t_np t_wild {;
qui summ `stat';
local bign = r(N);
sort `stat';
qui replace n = _n;
qui summ n if abs(`stat'-$maint)< 0.000001;
local myp = r(mean) / `bign';
global pctile_`stat' = 2*min(`myp', (1-`myp'));
};
*;

global mainp = norm($maint);
global pctile_main = 2*min($mainp, (1-$mainp));

local myfmt = "%7.5f";

di;
di "Number BS Reps = $bootreps, Null hypothesis = `hypothesis'";
display "Main beta" _column(13) "main T" _column(22) "Main %le" 
		_column(33) "NP BS %le" _column(44) "wild %le";
di %6.3f $mainbeta _column(13) %6.3f $maint _column(23) 
		`myfmt' $pctile_main _column(34) `myfmt' $pctile_t_np 
		_column(44) `myfmt' $pctile_t_wild ;
		
end;

global bootreps = 999;
runme 0;
